﻿/**
 * This jQuery plugin displays pagination links inside the selected elements.
 *
 * @author mojiahui
 * @version 0.1
 * @date 2011-1-9
 * @param {Object} opts Several options (see README for documentation)
 * @return {Object} jQuery Object
 */
 (function($){
	
	$.fn.pagination = function(opts){
		
		opts = jQuery.extend({
			totalNum : 0,				//记录总条数
			totalPage : 0,				//总页数
			currentPage : 0,			//当前第几页
			displayNum : 5,				//显示可选择的页数
			edgeNum : 1,				//左右两边的页数
			style : 'sabrosus',			//分页样式
			callback:function(){return false;}
		},opts||{});
		
		var createPageItem = function(pageNo){
			var page = $("<li></li>");
			if(opts.currentPage == pageNo){
				page.addClass("current").append("<span>"+pageNo+"</span>");
			} else {
				var link = $("<a href='javascript:void(0)'>"+pageNo+"</a>");
				link.click(function(){
					opts.callback(pageNo);
				});
				page.append(link);
			}
			return page;
		};
		var createPrev = function(){
			var prev = $("<li></li>");
			if(opts.currentPage == 1){
				prev.addClass("disabled").append("<span>上一页</span>");
			} else {
				var link = $("<a href='javascript:void(0)'>上一页</a>");
				link.click(function(){
					opts.callback(opts.currentPage - 1);
				});
				prev.append(link);
			}
			return prev;
		};
		var createNext = function(){
			var next = $("<li></li>");
			if(opts.currentPage == opts.totalPage){
				next.addClass("disabled").append("<span>下一页</span>");
			} else {
				var link = $("<a href='javascript:void(0)'>下一页</a>");
				link.click(function(){
					opts.callback(opts.currentPage + 1);
				});
				next.append(link);
			}
			return next;
		};
		
		var getPageRange = function(){
			var ne_half = Math.floor(opts.displayNum/2);
			var np = opts.totalPage;
			var upper_limit = np - opts.displayNum;
			var start = opts.currentPage > ne_half ? Math.max( Math.min(opts.currentPage - ne_half, upper_limit), 1 ) : 1;
			var end =  opts.currentPage > ne_half ? Math.min( opts.currentPage+ne_half + (opts.displayNum % 2), np) : Math.min(opts.displayNum, np);
			return {start:start, end:end};
		};
		
		var renderRange = function(startNo, endNo, container){
			for(var i=startNo;i<=endNo;i++){
				container.append(createPageItem(i));
			}
		};
		
		return this.each(function() {
			if(opts.totalPage <= 1) {
				$(this).empty();
				return;
			}
			var container = $("<ul class='pagination'></ul>");
			container.addClass(opts.style);
			container.append(createPrev());

			var pageRange = getPageRange();
			var begin,end;
			//最左边页数
			if (pageRange.start > 1) {
				end = Math.min(opts.edgeNum, pageRange.start);
				renderRange(1, end, container);
				if(opts.edgeNum < pageRange.start)
				{
					container.append("<li>...</li>");
				}
			}
			//中间页数
			renderRange(pageRange.start, pageRange.end, container);
			//最右边页数
			if (pageRange.end <= opts.totalPage) {
				if(opts.totalPage - opts.edgeNum > pageRange.end ){
					container.append("<li>...</li>");
				}
				begin = Math.max(opts.totalPage - opts.edgeNum, pageRange.end);
				renderRange(begin + 1, opts.totalPage, container);
			}
			container.append(createNext());
			$(this).empty().append(container);
		});
	};
	
})(jQuery);
